package com.fleet.project01.data.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    private static final String BASE_URL = "https://jsonplaceholder.typicode.com";
//    private static final String BASE_URL = "https://api.github.com";
    private static Retrofit retrofit;
    private static OkHttpClient okHttpClient;

    public static void init() {
        // 配置OkHttp
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                // 添加日志拦截器
                .addInterceptor(new LoggingInterceptor())
                .build();

        // 配置Gson
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        // 配置Retrofit
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }

    public static ApiService getApiService() {
        if (retrofit == null) {
            init();
        }
        return retrofit.create(ApiService.class);
    }
}
